package com.jingtai.shop.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.common.webfacade.BasePageBO;
import com.jingtai.common.webfacade.IdBO;
import com.jingtai.core.entity.member.MemberAddress;
import com.jingtai.shop.common.template.ShopBaseController;
import com.jingtai.shop.service.user.MemberAddressService;
import com.jingtai.shop.webfacade.bo.address.SaveAddressBO;
import com.jingtai.shop.webfacade.bo.address.UpdateAddressBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


/**
 * @author GR
 */
@Api(tags = "商城-用户地址")
@RestController
@RequestMapping("/shop/memberAddress")
@CrossOrigin(origins = "*")
public class MemberAddressController extends ShopBaseController {

    @Resource
    private MemberAddressService memberAddressService;

    /**
     * 查询用户地址分页列表
     */
    @ApiOperation(value = "查询用户地址分页列表")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/pageMemberAddress")
    public Map<String, Object> pageMemberAddress(@RequestBody BasePageBO bo) {
        bo.validate();
        Page<MemberAddress> memberAddressPage = memberAddressService.pageMemberAddress(getUserId(), bo.getPageNo(), bo.getPageSize());
        return returnResultMap(ResultMapInfo.GETSUCCESS, memberAddressPage);
    }

    /**
     * 添加用户地址
     */
    @ApiOperation(value = "添加用户地址")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version", "bo.memberId"})
    @PostMapping("/saveMemberAddress")
    public Map<String, Object> saveMemberAddress(@RequestBody SaveAddressBO bo) {
        bo.validate();
        boolean saveStatus = memberAddressService.saveMemberAddress(bo, getUserId());
        return returnResultMap(saveStatus ? ResultMapInfo.OPERATE_SUCCESS : ResultMapInfo.OPERATE_FAIL);
    }

    /**
     * 设置默认地址
     */
    @ApiOperation(value = "设置默认地址")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/setDefaultAddress")
    public Map<String, Object> setDefaultAddress(@RequestBody IdBO bo) {
        bo.validate();
        boolean setStatus = memberAddressService.setDefaultAddress(getUserId(), bo.getId());
        return returnResultMap(setStatus ? ResultMapInfo.OPERATE_SUCCESS : ResultMapInfo.OPERATE_FAIL);
    }

    /**
     * 删除用户地址
     */
    @ApiOperation(value = "删除用户地址")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/deleteMemberAddress")
    public Map<String, Object> deleteMemberAddress(@RequestBody IdBO bo) {
        bo.validate();
        boolean deleteStatus = memberAddressService.deleteMemberAddress(bo.getId());
        return returnResultMap(deleteStatus ? ResultMapInfo.OPERATE_SUCCESS : ResultMapInfo.OPERATE_FAIL);
    }

    /**
     * 根据id查询用户地址
     * 查询地址信息--地址修改
     * @param bo 用户地址主键id
     * @return
     */
    @ApiOperation(value = "根据id查询用户地址")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/queryMemberAddress")
    public Map<String, Object> queryMemberAddress(@RequestBody IdBO bo) {
        bo.validate();
        MemberAddress memberAddress = memberAddressService.queryMemberAddress(bo.getId());
        return returnResultMap(ResultMapInfo.OPERATE_SUCCESS, memberAddress);
    }

    /**
     * 修改用户地址
     */
    @ApiOperation(value = "修改用户地址")
    @ApiOperationSupport(ignoreParameters = {"bo.platform", "bo.version"})
    @PostMapping("/updateMemberAddress")
    public Map<String, Object> updateMemberAddress(@RequestBody UpdateAddressBO bo) {
        bo.validate();
        boolean updateStatus = memberAddressService.updateMemberAddress(bo);
        return returnResultMap(updateStatus ? ResultMapInfo.OPERATE_SUCCESS : ResultMapInfo.OPERATE_FAIL);
    }

    /**
     * 根据id查询用户地址
     * 订单支付查询
     */
    @ApiOperation(value = "根剧用户id查询默认地址")
    @PostMapping("/queryDefaultAddress")
    public Map<String, Object> queryDefaultAddress() {
        MemberAddress memberAddress = memberAddressService.queryDefaultAddress(getUserId());
        return returnResultMap(ResultMapInfo.OPERATE_SUCCESS, memberAddress);
    }

}
